package com.gjszn.kh.service;

import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gjszn.kh.context.MemberContext;
import com.gjszn.kh.dao.AdminMapper;
import com.gjszn.kh.dao.MemberMapper;
import com.gjszn.kh.model.Admin;
import com.gjszn.kh.model.Member;
import com.gjszn.kh.model.MyBatisParameterBean;
import com.gjszn.kh.utils.DateUtil;
import com.gjszn.kh.utils.MD5Kit;

/**    
* @Title: MemberSerivce.java  
* @Package com.gjszn.kh.service    
* @author jys@nx-yt.com    
* @date 2016年1月5日 下午5:14:22  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
@Service
public class MemberService {
	private static Logger logger = LogManager.getRootLogger();
	
	@Inject
	private MemberMapper memberMapper;
	
	@Inject
	private AdminMapper adminMapper;
	
	public boolean add(Member member){
		if(getByMember(member.getNumber()) != null){
			return false;
		}
		return memberMapper.insertSelective(member)==1;
	}
	
	public boolean addOrUpdate(Member member){
		if(getByMember(member.getNumber()) != null){
			return update(member);
		}
		if(memberMapper.insertSelective(member) == 1){
			logger.info(member.getName() + ":插入成功");
			MemberContext.addMember(member);
			
			Admin admin = new Admin();
			admin.setName(member.getNumber());
			admin.setPassword(MD5Kit.getMD5String("abc@123"));
			admin.setDate(DateUtil.getNowDateTime());
			adminMapper.insert(admin);
			
			return  true;
		}
		logger.info(member.getName() + ":信息插入失败");
		return  false;
	}
	
	public boolean update(Member member){
		if(memberMapper.updateByPrimaryKeySelective(member) == 1){
			logger.info(member.getName() + ":信息修改成功");
			MemberContext.updateMember(member);
			return  true;
		}
		logger.info(member.getName() + ":信息修改失败");
		return  false;
	}
	
	public Member getByMember(String memberNo){
		return memberMapper.selectByPrimaryKey(memberNo);
	}
	
	public boolean delete(String number){
		if(memberMapper.deleteByPrimaryKey(number) == 1){
			logger.info("会员[" + number +"]信息删除成功");
			MemberContext.deleteMember(number);
			return true;
		}
		logger.info("会员[" + number +"]信息删除失败");
		return true;
	}
	
	public List<Member> list(){
		List<Member> members = memberMapper.selectAll();
		logger.info("加载会员信息" + members.size());
		return members;
	} 
	
	public List<Member> listForPage(String sortColumnName, boolean isAsc){
		String dir = isAsc ? "ASC":"DESC";
		MyBatisParameterBean batisParameterBean = new MyBatisParameterBean(sortColumnName, dir, null, null);
		return memberMapper.selectForPage(batisParameterBean);
	}
	
	public List<Member> listForPage(int page, int rows){
		int count = rows>0 ? rows : 10;
		int start = page > 1 ? (page-1)*rows : 0;
		MyBatisParameterBean batisParameterBean = new MyBatisParameterBean(null, null, start, count);
		return memberMapper.selectForPage(batisParameterBean);
	}
	
	public List<Member> listForPage(String sortColumnName, boolean isAsc, int page, int rows){
		String dir = isAsc ? "ASC":"DESC";
		int count = rows>0 ? rows : 10;
		int start = page > 1 ? (page-1)*rows : 0;
		MyBatisParameterBean batisParameterBean = new MyBatisParameterBean(sortColumnName, dir, start, count);
		return memberMapper.selectForPage(batisParameterBean);
	}
	
	public int total(){
		return memberMapper.total();
	}
}

